package cn.swust.designpatternlearningplatform.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.swust.designpatternlearningplatform.common.AnnotationUtils.Authentication;
import cn.swust.designpatternlearningplatform.common.Result.R;
import cn.swust.designpatternlearningplatform.common.utils.AuthUserContext;
import cn.swust.designpatternlearningplatform.domain.bo.AnswerBo;
import cn.swust.designpatternlearningplatform.domain.dto.TopicJudgeDto;
import cn.swust.designpatternlearningplatform.domain.vo.TopicJudgeVo;
import cn.swust.designpatternlearningplatform.service.TopicJudgeService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/topic/judge")
public class TopicJudgeController {
    @Resource
    private TopicJudgeService topicJudgeService;

    /**
     * 判题
     *
     * @param topicJudgeVos
     * @return cn.swust.designpatternlearningplatform.common.Result.R<java.util.List < cn.swust.designpatternlearningplatform.domain.dto.TopicJudgeDto>>
     * @author Tang
     * @date 2023/9/26 11:19
     **/
    @Authentication
    @PostMapping
    public R<List<TopicJudgeDto>> judgeTopic(@RequestBody List<TopicJudgeVo> topicJudgeVos) {
        List<TopicJudgeDto> topicJudgeDtos = topicJudgeService
                .judgeTopic(AuthUserContext.get().getNumber(), convertListTopicJudgeBo(topicJudgeVos));
        return R.success(topicJudgeDtos);
    }

    private List<AnswerBo> convertListTopicJudgeBo(List<TopicJudgeVo> topicJudgeVos) {
        return topicJudgeVos.stream().map(i -> {
            AnswerBo topicJudgeBo = new AnswerBo();
            topicJudgeBo.setTopicId(i.getTopicId());
            String answer = String.join("",i.getOwnAnswer());
            topicJudgeBo.setOwnAnswer(answer);
            return topicJudgeBo;
        }).collect(Collectors.toList());
    }
}
